namespace GenericControls {
    using System;
    using System.Collections;
    using System.Web.UI;

    // Custom ControlBuilder that instantiates the generic control when the page
    // is constructed.
    public class GenericControlBuilder : ControlBuilder {
        public override void Init(TemplateParser parser, ControlBuilder parentBuilder, Type type,
            string tagName, string id, IDictionary attribs) {

            Type newType = type;

            if (attribs.Contains("objecttype")) {
                // If objecttype is specified, create a generic type that is bound to that
                // argument and then hide the objecttype attribute.
                Type genericType = typeof(GenericControl<>);
                Type genericArg = Type.GetType((string)attribs["objecttype"], true, true);
                newType = genericType.MakeGenericType(genericArg);
                attribs.Remove("objecttype");
            }

            base.Init(parser, parentBuilder, newType, tagName, id, attribs);
        }
    }
}
